package com.zxl.utils;

import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @packageName com.zxl.utils
 * @ClassName
 * @Author zxl
 * @Description //TODO
 * @date
 **/
@Component
public class StringUtils {

    @Autowired
    private StringRedisTemplate redisTemplate;

    public String createId(Integer id) {
        String date = new SimpleDateFormat("yyyyMMdd").format(new Date());
        String random = RandomStringUtils.random(5, false, true);
        String uid = getId(id);
        Long increment = redisTemplate.opsForValue().increment("auto-id-key");
        return date+random+uid+increment;
    }

    public String getId(Integer id) {
        StringBuffer temp = new StringBuffer();
        String s = id + "";
        if (temp.length() < 4) {
            for (int i = 0; i < 4 - s.length(); i++) {
                temp.insert(0, "0");
            }
        }
        temp.append(id);
        return temp.toString().substring(temp.length() - 4);
    }



}
